package com.weixing.oauth2.common.oauth2.integration.authenticator.password;

import com.weixing.oauth2.common.oauth2.integration.IntegrationAuthentication;
import com.weixing.oauth2.common.oauth2.integration.authenticator.AbstractPreparableIntegrationAuthenticator;
import com.weixing.oauth2.common.userdetails.IUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 默认登录处理
 * @author LIQIU
 * @date 2018-3-31
 **/
@Component
@Primary
public class UsernamePasswordAuthenticator extends AbstractPreparableIntegrationAuthenticator {

    @Autowired
    private IUserDetailsService customUserDetailsService;

    @Override
    public UserDetails authenticate(IntegrationAuthentication integrationAuthentication, HttpServletRequest request) {
       return  customUserDetailsService.loadUserByUsername(integrationAuthentication.getUsername());
    }

    @Override
    public void prepare(IntegrationAuthentication integrationAuthentication, HttpServletRequest request) {
    }

    @Override
    public boolean support(IntegrationAuthentication integrationAuthentication) {
        return StringUtils.isEmpty(integrationAuthentication.getAuthType());
    }

    @Override
    public void complete(IntegrationAuthentication integrationAuthentication, HttpServletRequest request) {
        complete(integrationAuthentication,request,customUserDetailsService);
    }
}
